#### Covid-19 Spillover Effects onto General Vaccine Attitudes
### Replication code - Over time vaccine hesitancy - Figures 1, S5, S8, S12
### Uses "covidspilloverdata.csv"
### Kristin Lunz Trujillo

library(dplyr)
library(haven)
library(srvyr)
library(survey)
library(tidyr)
library(broom)
library(purrr)
library(ggplot2)
library(stargazer)
library(dotwhisker)
library(tibble)
library(xtable)
library(jtools)
library(Hmisc)
library(ggpubr)

## load data
waves1418_cl <- read.csv(".../covidspilloverdata.csv")



# clean data
waves1418_cl <- waves1418_cl %>%
  mutate(
    vaxhes1 = case_when(vac_gen_1 == 7 ~ 0,
                        vac_gen_1 == 6 ~ 1,
                        vac_gen_1 == 5 ~ 2,
                        vac_gen_1 == 4 ~ 3,
                        vac_gen_1 == 1 ~ 4),
    vaxhes2 = case_when(vac_gen_2 == 1 ~ 0,
                        vac_gen_2 == 4 ~ 1,
                        vac_gen_2 == 5 ~ 2,
                        vac_gen_2 == 6 ~ 3,
                        vac_gen_2 == 7 ~ 4),
    vaxhes3 = case_when(vac_gen_3 == 7 ~ 0,
                        vac_gen_3 == 6 ~ 1,
                        vac_gen_3 == 5 ~ 2,
                        vac_gen_3 == 4 ~ 3,
                        vac_gen_3 == 1 ~ 4),
    pid3 = case_when(party7 > 4 ~ "Democrat",
                     party7 < 4 ~ "Republican",
                     party7 == 4 ~ "Independent"),
    date = case_when(study == 14 ~ "Dec 20",
                     study == 16 ~ "Feb 21",
                     study == 17 ~ "Apr 21",
                     study == 18 ~ "Jun 21",
                     study == 20 ~ "Nov 21",
                     study == 21 ~ "Jan 22",
                     study == 22 ~ "Mar 22")
  )

waves1418_cl$insttrust = (waves1418_cl$inst_trust1_7+waves1418_cl$inst_trust1_9+waves1418_cl$inst_trust2_2+waves1418_cl$inst_trust2_3)
table(waves1418_cl$insttrust)

waves1418_cl <- waves1418_cl %>%
  mutate(
    insttrust_step = case_when(insttrust == 16 ~ "A. High Trust",
                               insttrust == 15 ~ "A. High Trust",
                               insttrust == 14 ~ "A. High Trust",
                               insttrust == 13 ~ "B. Medium-High Trust",
                               insttrust == 12 ~ "B. Medium-High Trust",
                               insttrust == 11 ~ "B. Medium-High Trust",
                               insttrust == 10 ~ "C. Medium-Low Trust",
                               insttrust == 9 ~ "C. Medium-Low Trust",
                               insttrust == 8 ~ "C. Medium-Low Trust",
                               insttrust == 7 ~ "D. Low Trust",
                               insttrust == 6 ~ "D. Low Trust",
                               insttrust == 5 ~ "D. Low Trust",
                               insttrust == 4 ~ "D. Low Trust"),
    Children2 = case_when(Children==111 ~ "Not Parents",
                          Children==112 ~ "Parents"))


waves1418_cl$vaxhes = (waves1418_cl$vaxhes1 + waves1418_cl$vaxhes2 + waves1418_cl$vaxhes3)

# descriptives for vaccine hesitancy
table(waves1418_cl$vaxhes)
hist(waves1418_cl$vaxhes)

# add in weight
dat_survey <- waves1418_cl %>% 
  srvyr::as_survey(weights = weight.nat)

# labels for dates
waves1418_cl$date <- factor(waves1418_cl$date, levels=c("Dec 20", "Feb 21", "Apr 21", "Jun 21", "Nov 21", "Jan 22", "Mar 22"))




###################################################################################
## Figure: Hesitancy by Partisanship (Figure 1)
waves1418_cl %>%
  filter(!is.na(vaxhes)) %>%
  filter(!is.na(pid3)) %>%
  ggplot(aes(date,vaxhes))+      
  stat_summary(geom = "line", fun = mean, position = "dodge") +
  stat_summary(geom = "point", fun = mean, position = "dodge") +
  stat_summary(fun.data = mean_cl_normal,  
               geom = "errorbar") +
  facet_wrap(~pid3)  +
  scale_y_continuous(name="General Vaccine Skepticism", limits=c(0, 8)) +
  xlab("") + theme_bw()  + theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1, size=12), axis.title.y=element_text(size=12),
                                 strip.text.x = element_text(size = 14))

###################################################################################
## Figure: Overall hesitancy (Figure S5)
waves1418_cl %>%
  filter(!is.na(vaxhes)) %>% # filter on non-missing values
  ggplot(aes(date,vaxhes))+      
  stat_summary(geom = "line", fun = mean, position = "dodge") +
  stat_summary(geom = "point", fun = mean, position = "dodge") +
  stat_summary(fun.data = mean_cl_normal,  
               geom = "errorbar")  +
  scale_y_continuous(name="General Vaccine Skepticism", limits=c(0, 8)) +
  xlab("") + theme_bw()  + theme(axis.text.x=element_text(size=13), axis.title.y=element_text(size=13))


###################################################################################
## Figure: Overall hesitancy by levels of trust (Figure S8)
waves1418_cl %>%
  filter(!is.na(vaxhes)) %>%
  filter(!is.na(insttrust_step)) %>%
  ggplot(aes(date,vaxhes))+      
  stat_summary(geom = "line", fun = mean, position = "dodge") +
  stat_summary(geom = "point", fun = mean, position = "dodge") +
  stat_summary(fun.data = mean_cl_normal,  
               geom = "errorbar") +
  facet_wrap(~insttrust_step)  +
  scale_y_continuous(name="General Vaccine Skepticism", limits=c(0, 8)) +
  xlab("") + theme_bw() + theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1, size=14), axis.title.y=element_text(size=14),
                                strip.text.x = element_text(size = 14))


###################################################################################
## Figure: Overall hesitancy by parental status (Figure S12)
waves1418_cl %>%
  filter(!is.na(vaxhes)) %>%
  filter(!is.na(Children2)) %>%
  ggplot(aes(date,vaxhes))+      
  stat_summary(geom = "line", fun = mean, position = "dodge") +
  stat_summary(geom = "point", fun = mean, position = "dodge") +
  stat_summary(fun.data = mean_cl_normal,  
               geom = "errorbar") +
  facet_wrap(~Children2)  +
  scale_y_continuous(name="General Vaccine Skepticism", limits=c(0, 8)) +
  xlab("") + theme_bw() + theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1, size=12), axis.title.y=element_text(size=12),
                                strip.text.x = element_text(size = 14))











